package ca.uwo.android.directoryservice;

import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FacultyData extends DirectorySearchData {	
	
	private static final long serialVersionUID = 5865742023817705597L;
	
	private Roles[] roles;

	public FacultyData(JSONObject data) throws JSONException {
		super(data);

		if(data.has("roles")) {
			JSONArray roleArray = data.getJSONArray("roles");
			int len = roleArray.length();
			
			roles = new Roles[len];
			
			for(int i = 0; i < len; i++) {
				JSONObject rolesData = roleArray.getJSONObject(i);
				roles[i] = new Roles(rolesData);
			}
		}
		else roles = null;
	}

	public Roles[] getRoles() {
		return roles;
	}

	@Override
	public boolean isFaculty() {
		return true;
	}

	@Override
	public boolean isStudent() {
		return false;
	}

	public class Roles implements Serializable{
		
		private static final long serialVersionUID = 1880762901977521710L;
		
		protected String department;
		protected Contacts[] contacts;
		protected int length;

		public Roles(JSONObject role) throws JSONException {
			boolean d = role.has("department");
			department = d ? role.getString("department") : null;
			
			d = role.has("contacts");
			if(d) {
				JSONArray contactArray = role.getJSONArray("contacts");
				
				length = contactArray.length();
				contacts = new Contacts[length];
				
				for(int i = 0; i < length; i++) {
					JSONObject contactData = contactArray.getJSONObject(i);
					contacts[i] = new Contacts(contactData);
				}
			}
			else contacts = null;
		}
		
		public String getDepartment() {
			return department;
		}
		
		public Contacts[] getContacts() {
			return contacts;
		}
		
		public class Contacts implements Serializable{
			
			private static final long serialVersionUID = 4974436742624302658L;
			
			protected String phoneNumber;
			protected String location;

			public Contacts(JSONObject contact) throws JSONException {
				location = contact.has("location") ? contact.getString("location") : null;
				phoneNumber = contact.has("phoneNumber") ? contact.getString("phoneNumber") : null;
			}
			
			public String getPhoneNumber() {
				return phoneNumber;
			}
			
			public String getLocation() {
				return location;
			}	
		}
	}
}
